package com.mego.tzadd.utils;

import java.io.Serializable;

public class LonLat implements Serializable {
    /**
     *
     */
    private static final long serialVersionUID = -870520749171665437L;
    public static final double INVALID_LON = 180.0;
    public static final double INVALID_LAT = 90.0;

    public static final double MINLAT = -90;
    public static final double MAXLAT = 90;
    public static final double MINLNG = -180;
    public static final double MAXLNG = 180;

    public LonLat(double lon, double lat) {
        dLon = lon;
        dLat = lat;
    }

    public LonLat() {
        dLon = INVALID_LON;
        dLat = INVALID_LAT;
    }

    private double dLon;//经度[-180,180]
    private double dLat;//纬度[-90,90]
    private String geoHash = "";

    public double GetLon() {
        return dLon;
    }

    public double GetLat() {
        return dLat;
    }

    public int SetLonLat(double lon, double lat) {
        dLon = lon;
        dLat = lat;
        return 1;
    }

    public void SetInvalid() {
        dLon = INVALID_LON;
        dLat = INVALID_LAT;
    }

    public boolean IsValid() {
        if (dLon == INVALID_LON || dLat == INVALID_LAT
                || (dLon == 0.0 && dLat == 0.0)) {
            return false;
        }

        if (dLon >= INVALID_LON || dLon <= -INVALID_LON)
            return false;

        if (dLat >= INVALID_LAT || dLat <= -INVALID_LAT)
            return false;
        return true;
    }

    public boolean IsSame(double lon, double lat) {
        final double Tolerance = 0.000003;

        double xLon = dLon - lon;
        double xLat = dLat - lat;

        if (xLon < 0)
            xLon = -xLon;
        if (xLat < 0)
            xLat = -xLat;
        return (xLon < Tolerance && xLat < Tolerance);
    }

    public static boolean IsInvalid(double Lon, double Lat) {
        if (Lon == INVALID_LON && Lat == INVALID_LAT) {
            return true;
        }

        return false;
    }

    public boolean isInRect(LonLat low, LonLat up) {
        if (dLon < low.dLon || dLon > up.dLon) {
            return false;
        }
        if (dLat < low.dLat || dLat > up.dLat) {
            return false;
        }
        return true;
    }

    public String getGeoHash() {
        return geoHash;
    }

    public void setGeoHash(String geoHash) {
        this.geoHash = geoHash;
    }
}
